package com.example.musicalarm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.example.musicalarm.util.WeekDay;

public class AlarmActivity extends Activity {
	
	private static final String TAG = "AlarmActivity";
	
	private List<WeekDay> selectedDates = new ArrayList<WeekDay>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm);
		
		// set the current time
		setTime();

		// setup the check box listener
		setupCheckboxListener();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_alarm, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void setTime(){
		Calendar now = Calendar.getInstance();
		int hour = now.get(Calendar.HOUR);
		int mins = now.get(Calendar.MINUTE);
		
		TextView timeData = (TextView)findViewById(R.id.time_data);
		timeData.setText( hour + ":" + mins);
	}
	
	private void setupCheckboxListener(){
		List<WeekDay> allDays = WeekDay.getAllDays();
		
		for(WeekDay weekDay : allDays){
			CheckBox checkBox = (CheckBox)findViewById(weekDay.getId());
			checkBox.setOnCheckedChangeListener(new CheckDateListener());
		}
	}
	
	class CheckDateListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if(isChecked){
				selectedDates.add(WeekDay.getWorkDayById(buttonView.getId()));
			}else{
				selectedDates.remove(WeekDay.getWorkDayById(buttonView.getId()));
			}
			StringBuilder sb = new StringBuilder("selected dates: ");
			for(WeekDay weekDay : selectedDates){
				sb.append(weekDay.getId()+"\n");
			}
			Log.d(TAG, sb.toString());
		}
		
	}

}
